#include "BlockingQueue.hpp"

void *consumer(void *args)
{
    BlockingQueue<int> *bq = static_cast<BlockingQueue<int> *>(args);
    // 消费
    while (1)
    {
        int date;
        bq->pop(&date);
        cout << "消费数据 : " << date << endl;
        sleep(1);
    }
    return nullptr;
}
void *producter(void *args)
{
    BlockingQueue<int> *bq = static_cast<BlockingQueue<int> *>(args);
    // 生产
    while (1)
    {
        int date = rand() % 10 + 1;
        bq->push(date);
        cout << "生产数据 : " << date << endl;
        sleep(1);
    }
    return nullptr;
}
int main()
{
    srand((unsigned long long)time(nullptr) ^ getpid());
    BlockingQueue<int> *bq = new BlockingQueue<int>();
    pthread_t c, p; // p生产者线程，c消费者线程
    pthread_create(&c, nullptr, consumer, bq);
    pthread_create(&p, nullptr, producter, bq);

    pthread_join(c, nullptr);
    pthread_join(p, nullptr);
    delete bq;
    return 0;
}